<%--
  Created by IntelliJ IDEA.
  User: sunhao
  Date: 2016/4/7 0007
  Time: 10:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML>
<html>
<head>
    <title>添加新闻</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link href="/static/bui/css/bs3/dpl.css" rel="stylesheet" type="text/css"/>
    <link href="/static/bui/css/bs3/bui.css" rel="stylesheet" type="text/css"/>
    <link href="../../../../ueditor/themes/default/css/ueditor.css" type="text/css" rel="stylesheet">
    <script type="text/javascript" src="/static/bui/js/jquery-1.8.1.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/ueditor/ueditor.all.js"></script>
    <script type="text/javascript" src="/static/bui/js/bui.js"></script>
</head>
<body>

<div class="content" style="margin: 15px">


    <!-- 搜索页 ================================================== -->

    <div class="row">

        <form id="searchForm" class="form-horizontal" tabindex="0" style="outline: none;">
            <div class="row">

                <div class="control-group span8">
                    <label class="control-label">开始时间：</label>

                    <div class="controls">
                        <input type="text" class="calendar" name="startDate" style="width: auto"/>
                    </div>
                </div>

                <div class="control-group span8">
                    <label class="control-label">结束时间：</label>

                    <div class="controls">
                        <input type="text" class="calendar" name="endDate" style="width: auto"/>
                    </div>
                </div>

                <div class="control-group span8">
                    <label class="control-label">文章标题：</label>

                    <div class="controls">
                        <input type="text" name="title" class="control-text">
                    </div>
                </div>

                <div class="control-group span8">
                    <label class="control-label">作者：</label>

                    <div class="controls">
                        <input type="text" name="auth" class="control-text">
                    </div>
                </div>

                <div class="form-actions span5">
                    <button id="btnSearch" type="submit" class="button button-primary">搜索</button>
                </div>
            </div>
        </form>
    </div>

    <div class="search-grid-container">
        <div id="grid">
        </div>

    </div>

    <script type="text/javascript">

        // 定义UEditor对应的主目录
        window.UEDITOR_HOME_URL = "${contextPath}/ueditor/";
        // 页面加载完成后载入UEditor
        window.onload = function () {
            window.UEDITOR_CONFIG.initialFrameWidth = 1010;
            window.UEDITOR_CONFIG.initialFrameHeight = 600;
            UE.getEditor('container');
            UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
            UE.Editor.prototype.getActionUrl = function (action) {
                if (action == "uploadimage") {
                    return "/news/uploadImage";
                } else {
                    return this._bkGetActionUrl.call(this, action);
                }
            }
        }

        BUI.use('common/search', function (Search) {
            var enumObj = {"0": "常规", "1": "置顶一","2": "置顶二","3": "置顶三","-1": "下线"},
                    enumObj1 = {"1": "新闻", "2": "活动"};

            var columns = [
                {title: 'ID', width: 100, sortable: true, dataIndex: 'id'},
                {
                    title: '创建日期',
                    sortable: true,
                    dataIndex: 'created',
                    width: 150,
                    renderer: BUI.Grid.Format.datetimeRenderer
                },
                {title: '名称', width: 500, sortable: true, dataIndex: 'title'},
                {title: '作者', width: 200, sortable: true, dataIndex: 'auth'},
                {
                    title: '类型',
                    width: 100,
                    sortable: true,
                    dataIndex: 'type',
                    renderer: BUI.Grid.Format.enumRenderer(enumObj1)
                },
                {
                    title: '状态',
                    width: 100,
                    sortable: true,
                    dataIndex: 'status',
                    renderer: BUI.Grid.Format.enumRenderer(enumObj)
                },
                {
                    title: '操作', width: 300, dataIndex: 'g', renderer: function (value, obj) {
                    var editStr = '<a href="/news/addNews/' + obj._id + '" ><span class="grid-command btn-edit" title="编辑信息">编辑</span></a>'
                    return editStr
                }
                }
            ]
            var store = Search.createStore('./list.json', {
                        proxy: {
                            save: { //也可以是一个字符串，那么增删改，都会往那么路径提交数据，同时附加参数saveType
                                addUrl: './edit.json',
                                updateUrl: './edit.json'
                            }
                            ,
                            method: 'POST'
                        },
                        autoSync: true,
                        pageSize: 10
                        //保存数据后，自动更新
                    }),
                    gridCfg = Search.createGridCfg(columns, {
                        tbar: {
                            items: [
                                {
                                    text: '<i class="icon-plus"></i>新建',
                                    btnCls: 'button button-small',
                                    handler: addFunction
                                },
                                {
                                    text: '<i class="icon-remove"></i>删除',
                                    btnCls: 'button button-small',
                                    handler: delFunction
                                }
                            ]
                        },
                        plugins: [BUI.Grid.Plugins.CheckSelection, BUI.Grid.Plugins.AutoFit] // 插件形式引入多选表格
                    });

            var search = new Search({
                        store: store,
                        gridCfg: gridCfg
                    }),
                    grid = search.get('grid');

            function addFunction() {
                window.location.href = '/news/addNews/0'
            }

            //删除操作
            function delFunction() {
                var selections = grid.getSelection();
                delItems(selections);
            }

            function delItems(items) {
                var ids = [];
                BUI.each(items, function (item) {
                    ids.push(item.id);
                });

                if (ids.length) {
                    BUI.Message.Confirm('确认要删除选中的记录么？', function () {
                        $.ajax({
                            url: './delete.json',
                            dataType: 'json',
                            data: {ids: ids.toString()},
                            type: "POST",
                            success: function (data) {
                                if (data.code == 2000) {
                                    search.load()
                                } else {
                                    BUI.Message.Alert("删除失败")
                                }
                            }
                        })
                    }, 'question');
                }
            }

        })
    </script>
</div>
</body>
</html>
